﻿Imports System.Net.Mail

Public Class MailManager
    Private mailServerSettings As MailServerSettings
    Private smtpServer As New SmtpClient
    Private mail As New MailMessage

    '===============================================================
    ' OverRidden Constructor of MailManager
    '---------------------------------------------------------------
    'Purpose : Overides the constructor of MailManager for the purpose
    '          of passing arguments. 
    '
    ' Author : Chatzigeorgiou Konstantinos, 3 January 2013
    '===============================================================
    Public Sub New(ByVal mailServerSettings As MailServerSettings)
        Me.mailServerSettings = My.Settings.MailServerSettings
        ConfigureMailServer()
    End Sub

    '===============================================================
    ' Configuring Mail Server
    '---------------------------------------------------------------
    'Purpose : Configures tha settings of the mail server. 
    '          
    ' Author : Chatzigeorgiou Konstantinos, 3 January 2013
    '===============================================================
    Public Sub ConfigureMailServer()
        smtpServer.Port = mailServerSettings.SmtpPort
        smtpServer.Host = mailServerSettings.SmtpHost
        smtpServer.EnableSsl = mailServerSettings.SmtpSsl

        If mailServerSettings.SmtpAuthentication Then
            smtpServer.Credentials = New Net.NetworkCredential(mailServerSettings.SmtpUsername, mailServerSettings.SmtpPassword)
        Else
            smtpServer.Credentials = New Net.NetworkCredential()
        End If
    End Sub

    '===============================================================
    ' Creating an email message
    '---------------------------------------------------------------
    'Purpose : Creates an email message.  
    '          
    ' Author : Chatzigeorgiou Konstantinos, 3 January 2013
    '===============================================================
    Public Sub CreateEmail(ByVal toAddress As String, ByVal subject As String, ByVal body As String)
        mail = New MailMessage()
        mail.From = New MailAddress(mailServerSettings.FromEmail)
        mail.To.Add(toAddress)
        mail.Subject = subject
        mail.Body = body
    End Sub

    '===============================================================
    ' Adding an attachment 
    '---------------------------------------------------------------
    'Purpose : Adds an attachment to the email message.      
    '          
    ' Author : Chatzigeorgiou Konstantinos, 3 January 2013
    '===============================================================
    Public Sub AddAttachment(ByVal filepath As String)
        Dim attachment As Attachment = New Attachment(filepath)
        mail.Attachments.Add(attachment)
    End Sub

    '===============================================================
    ' Sending email message
    '---------------------------------------------------------------
    'Purpose : Sends the email message.    
    '          
    ' Author : Chatzigeorgiou Konstantinos, 3 January 2013
    '===============================================================
    Public Function SendEmail()
        Try
            smtpServer.Send(mail)
            Return "Done"
        Catch ex As Exception
            Return "Fail!"
        End Try
    End Function

    '===============================================================
    ' Creating and Sending email message with an attachment 
    '---------------------------------------------------------------
    'Purpose : Creates an email message, adds an attachment and sends it.      
    '          
    ' Author : Chatzigeorgiou Konstantinos, 3 January 2013
    '===============================================================
    Public Function SendEmailWithAttachment(ByVal toAddress As String, ByVal subject As String, ByVal body As String, ByVal filepath As String)
        CreateEmail(toAddress, subject, body)
        AddAttachment(filepath)
        Return SendEmail()
    End Function

    '===============================================================
    ' Creating and Sending email message 
    '---------------------------------------------------------------
    'Purpose : Creates an email message and sends it.      
    '          
    ' Author : Chatzigeorgiou Konstantinos, 6 January 2013
    '===============================================================
    Public Function SendEmail(ByVal toAddress As String, ByVal subject As String, ByVal body As String)
        CreateEmail(toAddress, subject, body)
        Return SendEmail()
    End Function
End Class
